import 'package:banner_carousel/banner_carousel.dart';
import 'package:camera/camera.dart';
import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/material.dart';
import 'package:yuanquan/routes/CameraPage.dart';
import 'package:yuanquan/routes/WebViewPage.dart';
import 'package:yuanquan/widgets/IconText.dart';

class HomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => HomePageState();
}

class HomePageState extends State<HomePage> {
  List<BannerModel> listBanners = [
    BannerModel(imagePath: 'https://img1.baidu.com/it/u=10309923,1927267730&fm=253&fmt=auto&app=138&f=JPEG?w=1200&h=500', id: "1"),
    BannerModel(imagePath: 'https://img.zcool.cn/community/01f35f5be192bca80121ab5d6598bc.jpg@2o.jpg', id: "2"),
    BannerModel(imagePath: 'assets/imgs/ic_logo.png', id: "3"),
  ];

  List<String> images = [
    'assets/imgs/ic_logo.png',
    'https://img1.baidu.com/it/u=10309923,1927267730&fm=253&fmt=auto&app=138&f=JPEG?w=1200&h=500',
    'https://img.zcool.cn/community/01f35f5be192bca80121ab5d6598bc.jpg@2o.jpg',
  ];

  Map<String, String> bannerUrls = {
    'assets/imgs/ic_logo.png' : 'https://kimi.moonshot.cn/',
    'https://img1.baidu.com/it/u=10309923,1927267730&fm=253&fmt=auto&app=138&f=JPEG?w=1200&h=500' : 'https://fanyi.baidu.com/mtpe-individual/multimodal?ext_channel=Aldtype#/',
    'https://img.zcool.cn/community/01f35f5be192bca80121ab5d6598bc.jpg@2o.jpg' : 'https://www.baidu.com/',
  };

  List<String> items = List.generate(100, (index) => 'Item ${index + 1}');

  @override
  void initState() {
    // init();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // appBar: AppBar(
      //   title: Text("home page"),
      // ),
      body: Stack(
        children: <Widget>[
          Positioned.fill(
            left: 16,
            top: 16,
            right: 16,
            bottom: 16,
            child: ListView(
              // itemExtent: 50,
              // shrinkWrap: true,
              children: [
                Text("data"),
                // BannerCarousel.fullScreen(
                //   banners: listBanners,
                //   customizedIndicators: IndicatorModel.animation(width: 20, height: 5, spaceBetween: 2, widthAnimation: 50),
                //   height: 120,
                //   activeColor: Colors.amberAccent,
                //   disableColor: Colors.white,
                //   animation: true,
                //   borderRadius: 10,
                //   // width: 250,
                //   indicatorBottom: false,
                //   onTap: (id) => print(id),
                // ),
                // SizedBox(height: 16,),
                CarouselSlider(
                  options: CarouselOptions(
                    height: 150.0,
                    autoPlay: true, // 开启自动滚动
                    autoPlayInterval: Duration(seconds: 3), // 自动滚动间隔时间
                    pauseAutoPlayOnTouch: true, // 触摸时暂停自动滚动
                    enableInfiniteScroll: true, // 无限循环
                    reverse: false, // 滚动方向，false为从左到右
                  ),
                  items: images.map((String i) {
                    return Builder(
                      builder: (BuildContext context) {
                        return GestureDetector(
                          child: Container(
                            width: MediaQuery.of(context).size.width,
                            margin: EdgeInsets.symmetric(horizontal: 5.0),
                            decoration: BoxDecoration(
                              color: Colors.black,
                              image: DecorationImage(
                                image: NetworkImage(i),
                                fit: BoxFit.cover,
                              ),
                            ),
                          ),
                          onTap:() {
                            Navigator.push(context, MaterialPageRoute(builder: (context){return WebviewPage(url: bannerUrls[i] ?? '',);}));
                          },
                        );
                      },
                    );
                  }).toList(),
                ),
                SizedBox(height: 16,),
                GridView.builder(
                  shrinkWrap: true,
                  physics: NeverScrollableScrollPhysics(),
                  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 5, // 每行的列数
                    crossAxisSpacing: 16.0, // 列间距
                    mainAxisSpacing: 16.0, // 行间距
                    childAspectRatio: 0.8, // 子项的宽高比
                  ),
                  itemCount: 10, // 项目总数
                  itemBuilder: (context, i) {
                    return Container(
                      // color: Color.fromARGB(255, 245, 196, 48),
                      child: FittedBox(
                        child: InkWell(
                          child: IconText(imgPath: 'assets/imgs/ic_logo.png', text: items[i]),
                          onTap: () {
                            // Navigator.push(context, MaterialPageRoute(builder: (context){return WebviewPage(url: i==0?'https://www.baidu.com':'https://pub.dev/',);}));
                            switch(i) {
                              case 0:
                              getCamera().then((onValue){
                                Navigator.push(context, MaterialPageRoute(builder: (context){return CameraPage(camera: onValue,);}));
                              });
                              break;
                            }
                          },
                        ),
                      ),
                    );
                  },
                ),
                SizedBox(height: 16,),
                Text("data"),
                ListView.builder(
                  physics: NeverScrollableScrollPhysics(),
                  shrinkWrap: true,
                  itemCount: 10, // 你可以根据需要设置项目的数量
                  itemExtent: 100,
                  itemBuilder: (context, i) {
                    return ListTile(
                      title: Container(color: Colors.blue, child: Text('Item $i'),),
                      onTap: () {
                        Navigator.push(context, MaterialPageRoute(builder: (context){return WebviewPage(url: i==0?'https://www.baidu.com':'https://pub.dev/',);}));
                      },
                    );
                  },
                ),
              ],
            ),
          ),
          // Positioned(
          //   bottom: 10.0,
          //   right: 10.0,
          //   child: FloatingActionButton(
          //     onPressed: () {},
          //     child: Icon(Icons.add),
          //   ),
          // ),
        ],
      ),
    );
  }

  Future<CameraDescription> getCamera() async {
    WidgetsFlutterBinding.ensureInitialized();
    // 获取设备上可用的相机列表
    final cameras = await availableCameras();
    // 获取第一个相机（通常是后置相机）
    final firstCamera = cameras.first;
    return firstCamera;
  }

}